package com.ag.improvedjava.iterable;

import java.util.Iterator;

final class CastIterable<E, R> implements Iterable<R> {

	private Iterable<E> iterable;
	private Class<R> c;
	
	CastIterable(Iterable<E> iterable, Class<R> c) {
		this.iterable = iterable;
		this.c = c;
	}

	@Override
	public Iterator<R> iterator() {
		return new CastIterator<>(iterable.iterator(), c);
	}
	
}

class CastIterator<E, R> extends UnmodifiableIterator<R> {
	
	private Iterator<E> iterator;
	private Class<R> c;

	CastIterator(Iterator<E> iterator, Class<R> c) {
		this.iterator = iterator;
		this.c = c;
	}

	@Override
	public boolean hasNext() {
		return iterator.hasNext();
	}

	@Override
	public R next() {
		return c.cast(iterator.next());
	}

}
